﻿using System;
using Gasco.Business.Services;
using Gasco.DataAccess;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Collections;
using System.Web.UI;
using System.IO;
using System.Web;
using System.Text;
using Mani.Common;


public partial class Controls_product_add : System.Web.UI.UserControl
{
    protected void Page_Init(object sender, EventArgs e)
    { 
      if(!Page.IsPostBack)
          LoadProductTypes();
    }

    
    protected void Page_Load(object sender, EventArgs e)
    {
      

        if (Request.QueryString["id"] != null)
        {
            if (!Page.IsPostBack)
            {
               
                ProductService service = new ProductService();
                product p = service.GetProductById(Convert.ToInt16(Request.QueryString["id"]));
                tbName.Text = p.name;
                ddProductType.SelectedValue = p.product_type_id.ToString();
                imgUploadImage.Src = String.Format("../getImage.ashx?ID={0}&w={1}&h={2}", Request.QueryString["id"], 130, 130);
                productImg.Style.Remove("display");
                fuUploadImage.Style.Add("display", "none");
                lbUploadImage.Style.Add("display", "none");
            }
        }
    }

    private void LoadProductTypes()
    {
        LookupService<product_type> ls = new LookupService<product_type>();
        ddProductType.DataSource = ls.GetAll();
        ddProductType.DataBind();
    }

    protected void btnclose_Click(object sender, EventArgs e)
    {
        InsertUpdateInfo();
        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script1", "<script>pageClose();</script>");
    }

    private void InsertUpdateInfo()
    {
        Type javascript = "javascript".GetType();
        bool checkUpload;
        if (fuUploadImage.HasFile)
        {
            string ext = Path.GetExtension(fuUploadImage.FileName).ToUpper();
            if (ext == ".BMP" || ext == ".PNG" || ext == ".GIF" || ext == ".JPG")
                checkUpload = true;
            else
                checkUpload = false;
        }
        else
        { checkUpload = true; }
       
            if (checkUpload)
                {
                    if (fuUploadImage.PostedFile.ContentLength < Convert.ToInt32(Resources.Resource.fileUploadSize))//جلوگیری از ارسال فایلهای با حجم بالا
                    {
                        bool InsertMode = (Request.QueryString["id"] == null);
                        ProductService service = new ProductService();
                        product p;

                        if (InsertMode)
                        {
                            p = new product();
                        }
                        else
                        {
                            p = service.GetProductById(Convert.ToInt32(Request.QueryString["id"]));
                        }

                        byte[] imageSize = new byte[fuUploadImage.PostedFile.ContentLength];
                        HttpPostedFile uploadedImage = fuUploadImage.PostedFile;
                        uploadedImage.InputStream.Read(imageSize, 0, (int)fuUploadImage.PostedFile.ContentLength);
                        p.name = tbName.Text;
                        p.product_type_id = Convert.ToInt32(ddProductType.SelectedValue);
                        p.update_date = DateTime.Now;

                        try
                        {
                        
                        if (InsertMode)
                        {
                            p.spec = imageSize;
                            p.create_date = DateTime.Now;
                            service.CreateProduct(p);
                        }
                        else
                        {
                            if (hidden.Value == "1")
                            {
                                p.spec = imageSize;
                            }
                            else
                            {
                                p.spec = p.spec;
                            }
                            service.UpdateProduct(p);
                        }
                       }
                        catch (DuplicateProduct)
                         {
                         UIHelper.ClientAlert(this.Page, GetLocalResourceObject("DuplicateProduct").ToString());
                         return;
                        }
                    }
                    else
                    {
                        Page.ClientScript.RegisterClientScriptBlock(javascript, "r2", "alert('سایز فایل شما نباید از 500 کیلوبایت بیشتر باشد.');'", true);
                    }
                }

                else
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("شما فقط قادر به ارسال فایلهایی با پسوند ");
                    sb.Append(" .JPG,.GIF,.PNG,.BMP");
                    sb.Append(" می باشید");
                    Page.ClientScript.RegisterClientScriptBlock(javascript, "r2", "alert('" + sb.ToString() + " ');", true);
                }
            }
            
        
        
}
       